package com.cakemallproject.interceptor;

import com.cakemallproject.Util.JwtUtil;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

@Component
public class LoginCheckInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        for (Cookie cookie : request.getCookies()) {
            if (cookie.getName().equals("token")) {
                try {
                    JwtUtil.parseToken(cookie.getValue());
                    return true;
                } catch (RuntimeException e) {
                    return false;
                }
            }
        }

        return false;
    }
}
